
package com.zcp.wizard;

import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Handler.Callback;
import android.os.Message;
import android.view.View;
import android.widget.RemoteViews;

import java.util.LinkedList;
import java.util.Timer;
import java.util.TimerTask;

public class MyBroadcastReceiver extends BroadcastReceiver {

    public static boolean isTestSwitcher = false;// 测试开关是否打开

    private static LinkedList<Activity> allActivity = new LinkedList<Activity>();

    private void finishAll() {
        for (Activity myActivity : allActivity) {
            myActivity.finish();
        }
        allActivity.clear();
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (Constants.EXIT_BROADCAST.equals(action)) {
            finishAll();
        } else if (action.equals("btnNext")) {
            WidgetUtils.showNext(context, AppWidgetManager.getInstance(context));

        } else if (action.equals("btnPrev")) {

            WidgetUtils.showPrev(context, AppWidgetManager.getInstance(context));
        } else if (action.equals("keyPower")) {
            if(Util.isGreenPrower(context))
            {
                return;
            }
            // 一键省电
            Util.keyPower(context);

            RemoteViews rv = WidgetUtils.buildUpdate(context);

            rv.setViewVisibility(R.id.txtBtryDialog, View.VISIBLE);
            rv.setTextViewText(R.id.txtBtryDialog,
                    context.getString(R.string.widget_battery_tips_powering));
            rv.setTextColor(R.id.txtBtryDialog, context.getResources().getColor(R.color.green));
            Util.updateWidget(context, rv);

            autoChangeTip(context, R.id.txtBtryDialog, context.getString(R.string.battery_tips_ok),
                    null);

        } else if (action.equals("freeMemory")) {
            // 释放内存
            Util.freeMemory(context);
            WidgetUtils.updateSystemStats(context, true);
        } else if (Intent.ACTION_MEDIA_UNMOUNTED.equals(action)
                || Intent.ACTION_MEDIA_MOUNTED.equals(action)
                || Intent.ACTION_MEDIA_BAD_REMOVAL.equals(action)
                || Intent.ACTION_MEDIA_REMOVED.equals(action)) {
            WidgetUtils.updateSystemStats(context, false);
        }
    }

    /**
     * 2秒后自动更新提示。
     * 
     * @param context
     */
    private void autoChangeTip(final Context context, final int viewId, final String content,
            final Callback callback) {
        final Timer time = new Timer();
        final TimerTask taskDisplay = new TimerTask() {

            @Override
            public void run() {
                RemoteViews rv = WidgetUtils.buildUpdate(context);
                rv.setViewVisibility(R.id.txtBtryDialog, View.INVISIBLE);
                Util.updateWidget(context, rv);
            }

        };
        TimerTask task = new TimerTask() {

            @Override
            public void run() {
                RemoteViews rv = WidgetUtils.buildUpdate(context);
                rv.setViewVisibility(viewId, View.VISIBLE);
                rv.setTextViewText(viewId, content);
                rv.setTextColor(viewId, context.getResources().getColor(R.color.green));
                Util.updateWidget(context, rv);
                if (callback != null) {
                    Message msg = new Message();
                    msg.obj = context;
                    callback.handleMessage(msg);
                }
                time.schedule(taskDisplay, 1000 * 3);
            }

        };
        time.schedule(task, 1000 * 3);
    }

    public static void register(Activity activity) {
        allActivity.add(activity);
    }

    public static void unRegister(Activity activity) {
        allActivity.remove(activity);
    }

}
